package com.zkteco.fingerprint.service;

import java.util.Base64;

/**
 * ZKFinger SDK代理类
 * 封装ZKFinger原生方法调用
 */
public class ZKFingerProxy {
    
    // ================== SDK常量定义 ==================
    public static final int ZKFP_ERR_OK = 0;
    public static final int ZKFP_ERR_INITLIB = -1;
    public static final int ZKFP_ERR_INIT = -2;
    public static final int ZKFP_ERR_NO_DEVICE = -3;
    public static final int ZKFP_ERR_NOT_SUPPORT = -4;
    public static final int ZKFP_ERR_INVALID_PARAM = -5;
    public static final int ZKFP_ERR_OPEN = -6;
    public static final int ZKFP_ERR_INVALID_HANDLE = -7;
    public static final int ZKFP_ERR_CAPTURE = -8;
    public static final int ZKFP_ERR_EXTRACT_FP = -9;
    public static final int ZKFP_ERR_ABSORT = -10;
    public static final int ZKFP_ERR_MEMORY_NOT_ENOUGH = -11;
    public static final int ZKFP_ERR_BUSY = -12;
    public static final int ZKFP_ERR_ADD_FINGER = -13;
    public static final int ZKFP_ERR_DEL_FINGER = -14;
    public static final int ZKFP_ERR_FAIL = -17;
    public static final int ZKFP_ERR_CANCEL = -18;
    public static final int ZKFP_ERR_VERIFY_FP = -20;
    public static final int ZKFP_ERR_MERGE = -22;
    public static final int ZKFP_ERR_NOT_OPENED = -23;
    public static final int ZKFP_ERR_NOT_INIT = -24;
    public static final int ZKFP_ERR_ALREADY_INIT = -25;
    
    // ================== 动态库加载 ==================
    static {
        try {
            // 加载ZKFinger SDK动态库
            System.loadLibrary("zkfinger");
            System.out.println("[SDK] ZKFinger动态库加载成功");
        } catch (UnsatisfiedLinkError e) {
            System.err.println("[错误] 无法加载ZKFinger动态库: " + e.getMessage());
            System.err.println("[提示] 请确保zkfinger.dll文件在系统路径中");
        }
    }
    
    // ================== SDK基础方法 ==================
    
    /**
     * 初始化指纹识别库
     */
    public static native int Init();
    
    /**
     * 终止指纹识别库
     */
    public static native int Terminate();
    
    /**
     * 获取连接的设备数量
     */
    public static native int GetDeviceCount();
    
    /**
     * 打开指定索引的设备
     */
    public static native long OpenDevice(int index);
    
    /**
     * 关闭设备
     */
    public static native int CloseDevice(long handle);
    
    // ================== 指纹操作方法 ==================
    
    /**
     * 采集指纹
     */
    public static native int AcquireFingerprint(long handle, byte[] imgBuffer, byte[] template, int[] templateLen);
    
    /**
     * 获取设备参数
     */
    public static native int GetParameters(long handle, int code, byte[] paramValue, int[] size);
    
    /**
     * 设置设备参数
     */
    public static native int SetParameters(long handle, int code, byte[] paramValue, int size);
    
    // ================== 数据库操作方法 ==================
    
    /**
     * 初始化指纹数据库
     */
    public static native long DBInit();
    
    /**
     * 释放指纹数据库
     */
    public static native int DBFree(long dbHandle);
    
    /**
     * 添加指纹到数据库
     */
    public static native int DBAdd(long dbHandle, int fpId, byte[] fpTemplate);
    
    /**
     * 从数据库删除指纹
     */
    public static native int DBDel(long dbHandle, int fpId);
    
    /**
     * 清空数据库
     */
    public static native int DBClear(long dbHandle);
    
    /**
     * 获取数据库中的指纹数量
     */
    public static native int DBCount(long dbHandle);
    
    /**
     * 1:1指纹比对
     */
    public static native int DBMatch(long dbHandle, byte[] template1, byte[] template2);
    
    /**
     * 1:N指纹识别
     */
    public static native int DBIdentify(long dbHandle, byte[] template, int[] fpId, int[] score);
    
    /**
     * 合并多个指纹模板
     */
    public static native int DBMerge(long dbHandle, byte[] template1, byte[] template2, byte[] template3, 
                                    byte[] regTemplate, int[] regTemplateLen);
    
    // ================== 工具方法 ==================
    
    /**
     * 将指纹模板数据转换为Base64字符串
     */
    public static String BlobToBase64(byte[] blob, int len) {
        if (blob == null || len <= 0) {
            return "";
        }
        
        byte[] data = new byte[len];
        System.arraycopy(blob, 0, data, 0, len);
        return Base64.getEncoder().encodeToString(data);
    }
    
    /**
     * 将Base64字符串转换为指纹模板数据
     */
    public static byte[] Base64ToBlob(String base64String) {
        if (base64String == null || base64String.isEmpty()) {
            return null;
        }
        
        try {
            return Base64.getDecoder().decode(base64String);
        } catch (IllegalArgumentException e) {
            System.err.println("[错误] Base64解码失败: " + e.getMessage());
            return null;
        }
    }
    
    /**
     * 获取错误描述
     */
    public static String getErrorDescription(int errorCode) {
        switch (errorCode) {
            case ZKFP_ERR_OK:
                return "操作成功";
            case ZKFP_ERR_INITLIB:
                return "算法库初始化失败";
            case ZKFP_ERR_INIT:
                return "设备初始化失败";
            case ZKFP_ERR_NO_DEVICE:
                return "未找到设备";
            case ZKFP_ERR_NOT_SUPPORT:
                return "设备不支持该操作";
            case ZKFP_ERR_INVALID_PARAM:
                return "无效参数";
            case ZKFP_ERR_OPEN:
                return "设备打开失败";
            case ZKFP_ERR_INVALID_HANDLE:
                return "无效的设备句柄";
            case ZKFP_ERR_CAPTURE:
                return "指纹采集失败";
            case ZKFP_ERR_EXTRACT_FP:
                return "指纹特征提取失败";
            case ZKFP_ERR_ABSORT:
                return "指纹模板无效";
            case ZKFP_ERR_MEMORY_NOT_ENOUGH:
                return "内存不足";
            case ZKFP_ERR_BUSY:
                return "设备忙";
            case ZKFP_ERR_ADD_FINGER:
                return "添加指纹失败";
            case ZKFP_ERR_DEL_FINGER:
                return "删除指纹失败";
            case ZKFP_ERR_FAIL:
                return "操作失败";
            case ZKFP_ERR_CANCEL:
                return "操作被取消";
            case ZKFP_ERR_VERIFY_FP:
                return "指纹验证失败";
            case ZKFP_ERR_MERGE:
                return "指纹模板合并失败";
            case ZKFP_ERR_NOT_OPENED:
                return "设备未打开";
            case ZKFP_ERR_NOT_INIT:
                return "SDK未初始化";
            case ZKFP_ERR_ALREADY_INIT:
                return "SDK已初始化";
            default:
                return "未知错误 (代码: " + errorCode + ")";
        }
    }
    
    /**
     * 检查操作结果是否成功
     */
    public static boolean isSuccess(int result) {
        return result == ZKFP_ERR_OK;
    }
    
    /**
     * 打印错误信息
     */
    public static void printError(String operation, int errorCode) {
        if (!isSuccess(errorCode)) {
            System.err.println("[错误] " + operation + " 失败: " + getErrorDescription(errorCode));
        }
    }
} 